
local Tool=require(script.Parent.Tool)
local function_list =_G.ServerOHOH.function_list
local M=_G.ServerOHOH.class.new(script,  _G.ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local gHp2Point=_G.CommonOHOH.config.Game.hp_Point_base
local  gRefundConst=_G.CommonOHOH.config.Game.ResetPointCost
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	

    self.player=self.parent
    self.DataStore=self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
    self.ItemCtrl=self.parent:GetComponent(_G.ServerOHOH.load.Server_ItemCtrl())
    self.RefundCost=gRefundConst
    
    self.values=Tool.CalculateAttributesPoint(self.DataStore:GetData("Points"))
    self:AddValuesMonitor()

    self.Event={
        lucky=self.player.LuckyPointChangeEvent,
        damge=self.player.DamgePointChangeEvent,
        speed=self.player.SpeedPointChangeEvent,
        hp=self.player.HpPointChangeEvent,

    }


    self.player.LevelChangeEvent:Connect(function(now,old)
            local d=now-old
            if d>0 then
                self.DataStore:GetData("Points").had= self.DataStore:GetData("Points").had+d
                self.parent:SynchDataToClient("Points")
            end

    end)

end;

function M.dtor(self)
	M.super.dtor(self);
end;


function M.refund(self)
    if not _G.CommonOHOH.load.ItemTool().IsHadEnoughByTable(self.RefundCost,self.DataStore) then
        return false
    end
    local real=self.DataStore:GetData("Points").__real
    for k,v in pairs(real) do
        if k=="had" then
            continue
        else
            real.had= real.had+v
            real[k]=0
        end
    end

    for k,v in pairs(self.RefundCost) do
        self.ItemCtrl:AddAnyThing(k,-v,true,false)
    end

    for _,e in pairs(self.Event) do
        e:Action()
    end
    self.values=Tool.CalculateAttributesPoint(self.DataStore:GetData("Points"))

    self.parent:SynchDataToClient("Points","base")

end


function M.add(self,id,count)
    if self.Event[id] then
        count=count or 1
        if self.DataStore:GetData("Points").had>=count then
    
            self.DataStore:GetData("Points").had=self.DataStore:GetData("Points").had-count
    
            if not  self.DataStore:GetData("Points")[id] then
                self.DataStore:GetData("Points")[id]=count
            else
                self.DataStore:GetData("Points")[id]=self.DataStore:GetData("Points")[id]+count
            end
            self.values=Tool.CalculateAttributesPoint(self.DataStore:GetData("Points"))
            self.parent:SynchDataToClient("Points")
            self.Event[id]:Action()
            return true
        else
            return false
        end

        
    end

end

function M.AddValuesMonitor(self)


    --训练加成
	self.player.TrainValueBaseEvent:Connect(function()
        -- print("训练加成---->",self.values.damge)
		return self.values.damge
	end)


	--攻击加成
	self.player.AttackValueBaseEvent:Connect(function()
		return self.values.damge
	end)
	
	--血量加成
	self.player.HpValueBaseEvent:Connect(function()
		return self.values.hp*gHp2Point
	end)

	--移动速度+点
	self.player.SpeedValueAddEvent:Connect(function()
		return self.values.speed
	end)

	-- 幸运值+点
	self.player.LuckyValueBaseEvent:Connect(function()
		return self.values.lucky
	end)

end


M:RegistFunction(function_list.points_refund,M.refund)

M:RegistFunction(function_list.points_add,M.add)


return M